/*
 * mpls_qos.h
 *
 *  Created on: 20/05/2011
 *      Author: avadiav
 */

#ifndef MPLS_QOS_H_
#define MPLS_QOS_H_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <omnetpp.h>

#include "MPLSPacket.h"

class PHB
	{
		float	 UpperDelayBound;
		float 	 UpperLossRatioBound;
		float 	 LinkCapacity;

		PHB(float Capacity) : UpperDelayBound(uniform(0,3)),
							  UpperLossRatioBound(uniform(0,0.01)),
							  LinkCapacity(Capacity)
		{};

		public:
		float getUpperDelayBound() 		{ return UpperDelayBound; 	  };
		float getUpperLossRatioBound()  { return UpperLossRatioBound; };
		float getLinkCapacity()         { return LinkCapacity; 		  };

	};

/** LSP Path class */
class LSP
{
private:
	int Capacity;
	int Bandwidth;
	int LSP_id;

	struct node
	{
		int index;
		int output;
		char label[4];
		node *next;
		node *previous;
	};

	node *head;
	node *tail;

public:

	LSP(int newCapacity, int newIndex, int newOutput,  const char * newLabel) : Capacity(newCapacity),
																						head(NULL),
																						tail(NULL)

	{
		head = (node*)malloc(sizeof(node));

		head->index = newIndex;
		head->output = newOutput;
		strcpy(head->label,newLabel);

		tail = head;

		head->next = tail->previous = NULL;
	};

	virtual ~LSP()
	{
		node *temp = head;

		while(temp)
		{
			head = head->next;
			free(temp);
			temp = head;
		}
	};

	//unprotected:
	int  			Get_Capacity() 					{ return Capacity; };
	int  			Get_Bandwidth() 				{ return Bandwidth; };
	void 			Set_Bandwidth(int NewBandwidth) { Bandwidth = NewBandwidth; };
	char 		   *Get_First() 					{ return head->label; }
	char		   *Get_Last()						{ return tail->label; }

	LSP * 	AddChain( const char *newLabel, int newIndex, int newOutput )
	{
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = head;

		while(Hopper->next!=NULL)
			Hopper = Hopper->next;


		node *NewChain;
		NewChain = (node*)malloc(sizeof(node));

		NewChain->index = newIndex;
		NewChain->output = newOutput;
		strcpy(NewChain->label,newLabel);

		NewChain->next = NULL;
		Hopper->next = NewChain;
		NewChain->previous = Hopper;

		tail = NewChain;

		return this;
	};

	char* GetLabel ( int Index ) {
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = head;

		while( Hopper )
		{
			if ( Index == Hopper->index )
				return Hopper->label;

			Hopper = Hopper->next;
		}
		return NULL;
	};

	int GetIndex ( char *Label ) {
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = head;

		while( Hopper )
		{
			if ( !strcmp(Label, Hopper->label) )
				return Hopper->index;

			Hopper = Hopper->next;
		}
		return -1;
	};


	int NextChain ( int Index ) {
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = head;

		while( Hopper )
		{
			if ( Index == Hopper->index )
				return ((Hopper->next) ? Hopper->next->output : -1);

			Hopper = Hopper->next;
		}
		return -1;
	};

	int NextChain ( char *Label ) {
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = head;

		while( Hopper )
		{
			if ( !strcmp(Label, Hopper->label) )
				return ((Hopper->next) ? Hopper->next->output : -1);

			Hopper = Hopper->next;
		}
		return -1;
	};

	int PreviousChain ( int Index ) {
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = tail;

		while( Hopper )
		{
			if ( Index == Hopper->index )
				return ((Hopper->previous) ? Hopper->previous->output : -1);

			Hopper = Hopper->previous;
		}
		return -1;
	};

	int PreviousChain ( char *Label ) {
		node *Hopper;
		Hopper=(node*)malloc(sizeof(node));
		Hopper = tail;

		while( Hopper )
		{
			if ( !strcmp(Label, Hopper->label) )
				return ((Hopper->previous) ? Hopper->previous->output : -1);

			Hopper = Hopper->next;
		}
		return -1;
	};

};
#endif /* MPLS_QOS_H_ */
